package com.hu.bgp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

import org.mortbay.util.IO;

/**
 * Tool om .jar bestand naar een bepaalde directory uit te pakken.
 * 
 * @author ejvos
 */
public class JarUtil
{
	public static void extract(File jarFile, File directory)
	{
		try
		{
			JarInputStream jin = new JarInputStream(new FileInputStream(jarFile));
			JarEntry entry = null;
			while ((entry = jin.getNextJarEntry()) != null)
			{
				File file = new File(directory, entry.getName());
				if (entry.isDirectory())
				{
					// make directory
					if (!file.exists())
						file.mkdirs();
				}
				else
				{
					// make directory (some jars don't list dirs)
					File dir = new File(file.getParent());
					if (!dir.exists())
						dir.mkdirs();

					// make file
					FileOutputStream fout = new FileOutputStream(file);
					IO.copy(jin, fout);
					fout.close();
				}
			}
			jin.close();
		}
		catch (IOException e)
		{
			// ignored
		}
	}
}
